function decodeText() {
    const input = document.getElementById('decode-input').value;
    const output = document.getElementById('decode-output');

    if (!input) {
        showDecodeMessage('请输入需要解码的文本', 'error');
        return;
    }

    try {
        let result = input;
        
        // 方法1: 尝试作为 JSON 字符串解析（处理已转义的情况）
        try {
            result = JSON.parse('"' + input + '"');
        } catch (e) {
            // 如果 JSON 解析失败，使用正则替换
            result = input.replace(/\\u([0-9a-fA-F]{4})/gi, (match, hex) => {
                return String.fromCharCode(parseInt(hex, 16));
            });
        }

        output.value = result;
        showDecodeMessage('解码成功', 'success');
    } catch (err) {
        showDecodeMessage(`解码失败: ${err.message}`, 'error');
        output.value = '';
    }
}

function showDecodeMessage(message, type = 'error') {
    const messageEl = document.getElementById('decode-message');
    messageEl.textContent = message;
    messageEl.className = `message ${type} show`;
    setTimeout(() => messageEl.classList.remove('show'), 3000);
}

async function copyDecodeResult() {
    const output = document.getElementById('decode-output').value;
    if (!output) {
        showDecodeMessage('没有可复制的内容', 'error');
        return;
    }

    try {
        await navigator.clipboard.writeText(output);
        showDecodeMessage('已复制到剪贴板', 'success');
    } catch (err) {
        showDecodeMessage('复制失败', 'error');
    }
}

function clearDecoder() {
    document.getElementById('decode-input').value = '';
    document.getElementById('decode-output').value = '';
    document.getElementById('decode-message').classList.remove('show');
}
